package com.daxia.ogmall.web.controller.m;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.daxia.ogmall.common.MyJSONObject;
import com.daxia.ogmall.common.NeedLogin;
import com.daxia.ogmall.dto.UserDTO;
import com.daxia.ogmall.dto.UserTaskDTO;
import com.daxia.ogmall.dto.UserTaskRecordDTO;
import com.daxia.ogmall.model.Task;
import com.daxia.ogmall.model.User;
import com.daxia.ogmall.support.MPage;
import com.daxia.ogmall.util.DateUtils;

@Controller
@RequestMapping(value = "/m/userTaskRecord", produces = "application/json;charset=UTF-8")
public class MUserTaskRecordController extends MBaseController {
	
	private static Logger logger = Logger.getLogger(MUserTaskRecordController.class);
    
	@NeedLogin
    @ResponseBody
    @RequestMapping("list")
    public String list(MPage page) throws Exception {
    	
    	UserDTO currentUser = getCurrentUserDTO();
    	User user = new User();
    	BeanUtils.copyProperties(currentUser, user);
    	
    	MyJSONObject data = new MyJSONObject();
    	JSONArray userTaskInfos = new JSONArray();
    	UserTaskDTO userTaskDTO = new UserTaskDTO();
    	User userQuery = new User();
    	userQuery.setId(user.getId());
    	userTaskDTO.setUser(userQuery);
		List<UserTaskDTO> userTaskList = userTaskService.find(userTaskDTO , null);
		for (UserTaskDTO ut : userTaskList) {
			MyJSONObject userTaskInfo = new MyJSONObject();
			JSONArray recordInfos = new JSONArray();
			
			UserTaskRecordDTO dto = new UserTaskRecordDTO();
			dto.setUser(userQuery);
			dto.setTask(ut.getTask());
			dto.setBeginDate(DateUtils.getBeginingOfAMonth(new Date()));
			dto.setEndDate(DateUtils.getEndOfAMonth(new Date()));
			List<UserTaskRecordDTO> userTaskRecordDTOs = userTaskRecordService.find(dto, page);
			for (UserTaskRecordDTO utr : userTaskRecordDTOs) {
				MyJSONObject userTaskRecord = new MyJSONObject();
				userTaskRecord.put("doneTime", DateUtils.format(utr.getCreateTime(), "yyyy-MM-dd"));
				userTaskRecord.put("taskId", utr.getTask().getId());
				userTaskRecord.put("id", utr.getId());
				//userTaskRecord.put("desc", "分享的一次朋友圈");
				recordInfos.add(userTaskRecord);
			}
			
			userTaskInfo.put("title", ut.getTask().getTitle());
			userTaskInfo.put("quota", ut.getQuota());
			userTaskInfo.put("done", userTaskRecordDTOs.size());
			userTaskInfo.put("taskId", ut.getTask().getId());
			userTaskInfo.put("userTaskRecordInfos", recordInfos);
			userTaskInfos.add(userTaskInfo);
			
		}
        data.put("userTaskInfos", userTaskInfos);
        super.setPageInfo(data, page);
        
        return toJson(data);
    }
	
	
	@NeedLogin
	@ResponseBody
	@RequestMapping("detail")
	public String detail(String taskId) throws Exception {
		super.assertNotBlank(taskId, "任务ID不能为空");
		
		MyJSONObject data = new MyJSONObject();
		JSONArray userTaskRecordInfos = new JSONArray();
		UserDTO currentUser = getCurrentUserDTO();
    	User user = new User();
    	BeanUtils.copyProperties(currentUser, user);
    	
    	UserTaskRecordDTO dto = new UserTaskRecordDTO();
		dto.setUser(user);
		Task task = new Task();
		task.setId(Long.parseLong(taskId));
		dto.setTask(task);
		dto.setBeginDate(DateUtils.getBeginingOfAMonth(new Date()));
		dto.setEndDate(DateUtils.getEndOfAMonth(new Date()));
		List<UserTaskRecordDTO> userTaskRecordDTOs = userTaskRecordService.find(dto, null);
		for (UserTaskRecordDTO utr : userTaskRecordDTOs) {
			MyJSONObject userTaskRecordInfo = new MyJSONObject();
			userTaskRecordInfo.put("doneTime", DateUtils.format(utr.getCreateTime(), "yyyy-MM-dd"));
			String headImage = "";
			String images = utr.getImages();
			String[] imgs = images.split(",");
			if (imgs.length > 0) {
				headImage = imgs[0];
			}
			userTaskRecordInfo.put("headImage", headImage);
			userTaskRecordInfo.put("imageInfos", imgs);
			userTaskRecordInfo.put("details", utr.getDetails());
			
			userTaskRecordInfos.add(userTaskRecordInfo);
		}
		data.put("done", userTaskRecordDTOs.size());
		data.put("userTaskRecordInfos", userTaskRecordInfos);
		return toJson(data);
	}
	
	@NeedLogin
	@ResponseBody
	@RequestMapping("save")
	public String save(String taskId, String content, HttpServletRequest request) throws Exception {
		super.assertNotBlank(taskId, "任务ID不能为空");

		String fileUrl = super.saveFilesAndReturnFileName(request);

		UserDTO currentUser = getCurrentUserDTO();
    	User user = new User();
    	BeanUtils.copyProperties(currentUser, user);
    	
    	Task task = taskService.load(Long.parseLong(taskId));
    	
    	UserTaskRecordDTO dto = new UserTaskRecordDTO();
    	dto.setUser(user);
    	dto.setTask(task);
    	dto.setDetails(content);
    	dto.setImages(fileUrl);
    	dto.setCreateTime(new Date());
		userTaskRecordService.create(dto );
		
		
		return toJson();
	}
    
}
